package cn.tom.config;

import cn.tom.tools.JwtUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
public class JwtLoginFilter extends UsernamePasswordAuthenticationFilter {
    public JwtLoginFilter() {
        super.setFilterProcessesUrl("/api/login");
    }
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request,
                                                HttpServletResponse response)
            throws AuthenticationException {
        String uname= super.obtainUsername(request);
        String pwd=super.obtainPassword(request);
        log.info("attemptAuthentication 用户="+uname+"; 密码="+pwd);
        UsernamePasswordAuthenticationToken authRequest =
                new UsernamePasswordAuthenticationToken(uname,pwd);
        return this.getAuthenticationManager().authenticate(authRequest);
    }
}
